/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer.ui.web;

import nl.sidn.enumdiscoverer.LogConfig;
import nl.sidn.enumdiscoverer.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Renders a web page. The URL of the page is passed in through an Intent
 * @author	Grant Patterson (grant.patterson@catalyst.net.nz)
 */
public class WebActivity extends Activity {

	static final String TAG = LogConfig.getLogTag(WebActivity.class);
	// whether DEBUG level logging is enabled (whether globally, or explicitly for this log tag)
	static final boolean DEBUG = LogConfig.isDebug(TAG);
	// whether VERBOSE level logging is enabled
	static final boolean VERBOSE = LogConfig.VERBOSE;
	
	
	private WebView mWebView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.web);

		mWebView = (WebView) findViewById(R.id.webview);
		mWebView.setWebViewClient(new Client());
		mWebView.getSettings().setJavaScriptEnabled(true);
		
		setUrlFromIntent(getIntent());
	}
	
	public void setUrlFromIntent(Intent intent) {
		
		if (DEBUG) Log.d(TAG, "Setting URL from intent " + intent);
		
		Uri url = intent.getData();
		
		if (url == null) {
			Log.w(TAG, "Expected a URL, put none was found");
		}
		
		mWebView.loadUrl(url.toString());
	}

	private class Client extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}
}